package com.laxcen.toydemo;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;

import util.BackgroundMusic;
import util.DefaultHandler;
import util.SoundClass;

public class GameStep6Activity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    HomeMenuButtonControl btn_home;
    HomeMenuButtonControl btn_option1, btn_option6;
    HomeMenuButtonControl btn_daxiao, btn_daku;
    HomeMenuButtonControl img_xiaoxiong_eat_apple;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_step6);
        mGameState.start();

        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);

        btn_option1 = findViewById(R.id.btn_option1);
        btn_option1.setOnClickListener(this);

        btn_option6 = findViewById(R.id.btn_option6);
        btn_option6.setOnClickListener(this);

        btn_daxiao = findViewById(R.id.btn_daxiao);
        btn_daxiao.setOnClickListener(this);
        btn_daku = findViewById(R.id.btn_daku);
        btn_daku.setOnClickListener(this);


        img_xiaoxiong_eat_apple = findViewById(R.id.img_xiaoxiong_eat_apple);
    }

    private void startRepeatPromptGbSound() {
        palyAudio1();
    }

    private void palyAudio1() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep6) {
                    BackgroundMusic.instance(GameStep6Activity.this).setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            palyAudio2();
                        }
                    }).play(R.raw.audio_game_step6_repeat_prompt, false);
                }
            }
        }, 2000);
    }

    private void palyAudio2() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep6) {
                    BackgroundMusic.instance(GameStep6Activity.this).setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            palyAudio3();
                        }
                    }).play(R.raw.audio_game_step6_repeat_prompt2, false);
//                    palyAudio3();
                }
            }
        }, 2000);
    }

    private void palyAudio3() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep6) {
                    BackgroundMusic.instance(GameStep6Activity.this).setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            palyAudio4();
                        }
                    }).play(R.raw.audio_game_step6_repeat_prompt3, false);
//                    palyAudio4();
                }
            }
        }, 2000);
    }

    private void palyAudio4() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep6) {
                    BackgroundMusic.instance(GameStep6Activity.this).play(R.raw.audio_game_step6_repeat_prompt2, false);
                }
            }
        }, 2000);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_apple1 || v.getId() == R.id.btn_apple2) {
            HomeMenuButtonControl btn_apple = (HomeMenuButtonControl) v;
//            btn_apple.shake();
        } else if (v.getId() == R.id.btn_home) {
            BackgroundMusic.instance(this).stop();
            startActivityAndFinish(GameStepSelectActivity.class);
        } else if (v.getId() == R.id.btn_option1) {
//            onOption1Active();
        }
    }

//    private void onOption1Active() {
////        btn_apple1.fall();
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//
////                img_xiaoxiong_eat_apple.show();
//                btn_daku.show();
//                btn_daku.bigSplash();
//                new SoundClass(GameStep6Activity.this).playSound(R.raw.audio_daku);
//            }
//        }, 1000);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                new SoundClass(GameStep6Activity.this).playSound(R.raw.audio_game_step6_error_prompt);
//            }
//        }, 4000);
//    }


    @Override
    protected void onPause() {
        super.onPause();
        BackgroundMusic.instance(this).stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.GameStep6);
        startRepeatPromptGbSound();
    }
}
